package com.koron.css2.serviceManage.impl;


import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserFieldBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.UserFieldConfigMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 用户字段配置修改
 * 
 * @author 甄玉晴
 *
 */
public class UserFieldConfigUpdate implements ServerInterface {

	private static Logger logger = LogManager.getLogger(UserFieldConfigUpdate.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			UserFieldBean bean = JsonUtils.objectToPojo(req.getData(), UserFieldBean.class);
			if(bean==null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "缺少参数", void.class);
			}
			//获取mapper
			UserFieldConfigMapper mapper = factory.getMapper(UserFieldConfigMapper.class);
			//根据编号查询用户字段是否已存在
			UserFieldBean code=mapper.findUserFieldConfig(bean);
			if(code!=null&&!code.getId().equals(bean.getId())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "已存在相同标识得字段", void.class);
			}
			//修改
			bean.setUpdateInfo(userInfo);;
			mapper.updateUserFieldConfig(bean);
		} catch (Exception e) {
			logger.error("非法参数", e);
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, "非法参数", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}
}
